export const createWebSocket = (url, token, roomId, sessionId,onMessage) => {



    const ws = new WebSocket(`${url}?token=${token}&room_id=${roomId}&session_id=${sessionId}`);

    ws.onopen = () => {
        console.log("WebSocket connection established");
    };

    ws.onmessage = (event) => {
        if (onMessage) {
            onMessage(JSON.parse(event.data));
        }
    };

    ws.onerror = (error) => {
        console.error("WebSocket error:", error);
    };

    return ws;
};

export const keepAlive = (ws, interval = 10000) => {
    let pingInterval = setInterval(() => {
        if (ws.readyState === WebSocket.OPEN) {
            ws.send(JSON.stringify({ type: "ping" }));
        }
    }, interval);

    return () => clearInterval(pingInterval);
};
